# pita: An invisible audio player
# Copyright (C) 2006-2007 Austin Bingham
#
# This program is free software# you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation# either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY# without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program# if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# You can reach the author at:
#     austin.bingham@gmail.com

from pita.decoder import *
from pyxiph.ao import *
import wave

class WAV(Decoder):
    def __init__(self, urlobj):
        Decoder.__init__(self)

        self.file = wave.open(urlobj, 'r')

        if not self.file:
            raise CTORError('unable to open file %s' % filename)

    def init_device(self):
        return Device(default_driver_id(),
                      format = Format(bits = self.file.getsampwidth() * 8,
                                      rate = self.file.getframerate(),
                                      channels = self.file.getnchannels(),
                                      byte_format = byte_format.little))
    
    def play_chunk_impl(self):
        buff = self.file.readframes(10000)
        if len(buff) == 0:
            return Decoder.FINISHED
        else:
            self.device.play(buff, len(buff))
            return Decoder.OK
